package medium;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 17. 电话号码的字母组合
 * 创建时间：2024-04-01 17:07
 */
public class 电话号码的字母组合_17 {
    static class Solution {
        Map<Character, String> phoneMap = new HashMap<Character, String>() {{
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }};

        public List<String> letterCombinations(String digits) {
            List<String> rsl = new ArrayList<>();
            if (digits.isEmpty()) {
                return rsl;
            }
            backtrack(rsl, digits, 0, new StringBuilder());
            return rsl;
        }

        public void backtrack(List<String> rsl, String digits, int index, StringBuilder combination) {
            if (digits.length() == index) {
                rsl.add(combination.toString());
            } else {
                char c = digits.charAt(index);
                String s = phoneMap.get(c); // 获得对应数字上的字母
                for (int i = 0; i < s.length(); i++) { // 遍历获取的字母
                    combination.append(s.charAt(i));
                    backtrack(rsl, digits, index + 1, combination);
                    combination.deleteCharAt(index); // 因为是组合，所以每次遍历即将结束，都要将这一次的add删除
                }
            }
        }

        public static void main(String[] args) {
            Solution s = new Solution();
            List<String> rsl = s.letterCombinations("23");
            System.out.println(rsl);

            String a = "1231222";
            a.indexOf("222");
        }
    }
}
